package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.Result;
import com.itheima.pojo.Order;
import com.itheima.service.InterventionService;
import com.itheima.service.OrderService;
import com.itheima.utils.DateUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/healthIntervention")
public class InterventionController {
    @Reference
    private OrderService orderService;
    @Reference
    private InterventionService interventionService;


    @RequestMapping("/findOrderAndSetmeal")
    public Result findOrderAndSetmeal(){
        try {
            List<Order> list= orderService.findOrderAndSetmealAll();
            return new Result(true, MessageConstant.GET_ORDER_LIST_SUCCESS,list);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_ORDER_LIST_FAIL);
        }
    }
    @RequestMapping("/findOrderAndSetmealByTime")
    public Result findOrderAndSetmealByTime(String startTime, String endTime, @RequestParam("value") String value) throws Exception {
        try {
            Date start = DateUtils.parseString2Date(startTime);
            Date end = DateUtils.parseString2Date(endTime);
            byte [] b=value.getBytes( "ISO-8859-1" );
            String str= new String(b, "utf-8" );
            System.out.println(str);
            List<Order> list=orderService.findOrderAndSetmealByTime(start,end,str);
            return new Result(true, MessageConstant.GET_ORDER_LIST_SUCCESS,list);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_ORDER_LIST_FAIL);
        }
    }

    //根据套餐ID查询套餐详细信息（包括套餐基本信息、套餐包含的检查组、检查组包含的检查项,健康干预师建议,姓名和建议运动）
    @RequestMapping("/findHealthInterventionByOrderId")
    public Result findHealthInterventionByOrderId(Integer orderId){
        System.out.println(orderId);
        try{
            Map<String,Object> map =interventionService.findHealthInterventionByOrderId(orderId);
            return new Result(true,"订单信息成功",map);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"查询订单信息失败");
        }
    }
}
